package com.source.labelAndBarcode.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.labelAndBarcode.entity.BarcodeGrant;
import com.source.labelAndBarcode.entity.BarcodeRevoke;
import com.source.labelAndBarcode.service.BarcodeRevokeService;
import com.source.labelAndBarcode.vo.BarcodeGrantVo;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import com.source.labelAndBarcode.vo.BarcodeRevokeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Joey
 * @since 2021-02-23
 */
@Api(tags = "条形码回收")
@RestController
@CrossOrigin
@RequestMapping("/labelAndBarcode/barcode-revoke")
public class BarcodeRevokeController {

    @Autowired
    private BarcodeRevokeService revokeService;

    @ApiOperation(value = "条形码回收")
    @PostMapping("/barcodeRevoke")
    public CommonResult<String> barcodeRevoke(@RequestBody BarcodeRevoke barcodeRevoke){
        if(revokeService.save(barcodeRevoke)){
            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

    @ApiOperation(value = "查看条形码回收")
    @GetMapping("/findBarcodeRecoveryAll")
    public CommonResult<List<BarcodeRevokeVo>> findAll(){
        List<BarcodeRevokeVo> list = revokeService.findAll();
        return new CommonResult<List<BarcodeRevokeVo>>(ResultEnum.SUCCESS,list);

    }

    @ApiOperation(value = "查看条形码回收", notes = "分页")
    @GetMapping("/findAllOnPagination")
    public CommonResult<IPage<BarcodeRevokeVo>> findOnPagination(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                                 @RequestParam(value = "size", defaultValue = "10") Integer size){
        Page<BarcodeRevokeVo> page = new Page<>(current, size);
        IPage<BarcodeRevokeVo> pages = revokeService.findAllOnPagination(page);
        return new CommonResult<IPage<BarcodeRevokeVo>>(ResultEnum.SUCCESS,pages);
    }

    @ApiOperation(value = "模糊查询条形码回收", notes = "分页")
    @GetMapping("/findByKeyWord")
    public CommonResult<IPage<BarcodeRevokeVo>> findByKeyWord(@RequestParam(value = "keyWord", required = false) String keyWord,
                                                             @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size){
        if(null == keyWord){
            return findOnPagination(current, size);
        }
        Page<BarcodeRevokeVo> page = new Page<BarcodeRevokeVo>(current, size);
        IPage<BarcodeRevokeVo> pages = revokeService.findByKeyWord(page, keyWord);
        return new CommonResult<IPage<BarcodeRevokeVo>>(ResultEnum.SUCCESS,pages);
    }

    @ApiOperation(value = "修改条形码回收")
    @PostMapping("/updateBarcodeRevoke")
    public CommonResult<String> updateBarcodeRevoke(@RequestBody BarcodeRevoke barcodeRevoke){
        if(revokeService.updateById(barcodeRevoke)){
            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

    @ApiOperation(value = "回显条形码回收")
    @GetMapping("/findBarcodeRecoveryById/{id}")
    public CommonResult<BarcodeRevokeVo> findOneById(@PathVariable String id){
        BarcodeRevokeVo barcodeRevoke = revokeService.findById(id);
        return new CommonResult<BarcodeRevokeVo>(ResultEnum.SUCCESS,barcodeRevoke);
    }

    @ApiOperation(value = "回显条形码发放（用于修改条形码发放）")
    @GetMapping("/findById/{id}")
    public CommonResult<BarcodeRevoke> findById(@PathVariable String id){
        BarcodeRevoke grant = revokeService.getById(id);
        return new CommonResult<BarcodeRevoke>(ResultEnum.SUCCESS,grant);
    }

    @ApiOperation(value = "删除条形码回收")
    @DeleteMapping("/deleteBarcodeRecoveryById/{id}")
    public CommonResult<String> deleteById(@PathVariable String id){
        if(revokeService.removeById(id)){
            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

    @ApiOperation(value = "批量删除条形码回收，id以英文逗号形式隔开")
    @DeleteMapping("/deleteBarcodeRecoveryByIds/{ids}")
    public CommonResult<String> deleteByIds(@PathVariable List<String> ids){
        try {
            if(revokeService.removeByIds(ids)){
                return new CommonResult<String>(ResultEnum.SUCCESS);
            }
            return new CommonResult<String>(ResultEnum.ERROR,"删除失败！");
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }



}

